/* img hotspot */

//cll load() ie8不起作用，自定义图片加载完成的方法
function imgOnload(image) {
  // 添加最大重试次数防止无限递归
  if (!imgOnload.attempts) {
      imgOnload.attempts = 0; 
  }
  
  if (image.complete || image.width) {
      imgOnload.attempts = 0; // 重置计数
      return image;
  } else {
      // 如果超过最大重试次数则返回
      if (imgOnload.attempts >= 50) { 
          imgOnload.attempts = 0;
          return image;
      }
      
      imgOnload.attempts++;
      return new Promise(resolve => {
          // 使用onload事件替代递归
          image.onload = () => {
              imgOnload.attempts = 0;
              resolve(image);
          };
      });
  }
}

/* 显示当前选中的热区 */
function showHot(id) {
    var hotFirst = 0;
    $("#hotspot .hot-location").find(".hot-item").css("border", "2px solid #216797");
    $("#hotspot .hot-location").find(".hot-item-pointer").css("background-color", "#216797");
    $(".hot-location").children(".hot-item").each(function () {
        var hotID = $(this).attr("id");
        if (hotID.indexOf("_") >= 0 && hotID.split("_")[0] == id) {
            if (hotFirst == 0) {
                hotFirst = hotID;
                hotCentre(hotID);
            }
            $("#hotspot .hot-location").find("#" + hotID).css("border", "2px solid #FF6666");
            $("#hotspot .hot-location").find("#" + hotID).find(".hot-item-pointer").css("background-color", "#FF6666");
        }
        if (hotID.indexOf("_") < 0 && hotID.split("_")[0] == id) {
            hotCentre(id);
            $("#hotspot .hot-location").find("#" + id).css("border", "2px solid #FF6666");
            $("#hotspot .hot-location").find("#" + id).find(".hot-item-pointer").css("background-color", "#FF6666");
        }
    })
}


$(function () {
    //img图片弹窗
    var hotHtml =
        '<div id="hotspot" style="overflow:hidden;display:none;position: relative; top: 0; left: 0; width: 50%; height: 100%;">';
    hotHtml +=
        '<div class="zoom" style="height:60px;width:100%;"><div class="zoom-img"><div class="zoom-minus"><img src="../img/minus.png" /></div><div class="zoomm-value">100%</div><div class="zoom-add"><img src="../img/add.png" /></div></div></div>';
    hotHtml += '<div class="hot_img_close"><img class="hot_img_close" src="../img/close.png" /></div>';
    hotHtml += '<div class="hot-img-content">';
    hotHtml += '<div class="hot-img-div" style="position: absolute;">';
    hotHtml += '<img class="hot-img" src="" />';
    hotHtml += '<span class="hot-location"></span>';
    hotHtml += '</div>';
    hotHtml += '</div>';
    hotHtml += '<div class="hot_img_zoom"><img class="hot_img_zoom" src="../img/image.png" /></div>';
    hotHtml += '</div>';
    $("#html").prepend(hotHtml);

    // $(".hot_img_zoom").css("right", $(window).width() / 2 + 60);
    // $(".hot_img_close").css("right", $(window).width() / 2 + 10);

    //svg图片弹窗
    var svgHotHtml =
        '<div id="svghotspot" style="overflow:hidden;display:none;position: relative; top: 0; left: 0; width: 50%; height: 100%;">';
    svgHotHtml +=
        '<div class="zoom" style="height:60px;width:100%;"><div class="zoom-img"><div class="svg-minus"><img src="../img/minus.png" /></div><div class="svg-value">100%</div><div class="svg-add"><img src="../img/add.png" /></div></div></div>';
    svgHotHtml +=
        '<div class="svg_img_close"><img class="svg_img_close" onclick="closeSvg()" src="../img/close.png" /></div>';
    svgHotHtml += '<div class="svg-img-content">';
    svgHotHtml += '<div class="svg-img-div" style="position: absolute;width:100%;height:100%;">';
    svgHotHtml +=
        '<object data="" class="svg-img" id="svg-img" style="width:100%;height:100%;" type="image/svg+xml" onload="zoomSvg(this)"></object>';
    svgHotHtml += '</div>';
    svgHotHtml += '</div>';
    svgHotHtml +=
        '<div class="svg_img_zoom"><img class="svg_img_zoom" onclick="svgShow()" src="../img/image.png" /></div>';
    svgHotHtml += '</div>';
    $("#html").prepend(svgHotHtml);

    // $(".svg_img_zoom").css("right", $(window).width() / 2 + 60);
    // $(".svg_img_close").css("right", $(window).width() / 2 + 10);


    /* 点击热区 */
    $("body").on("click", ".hot-location>.hot-item", function () {
        var id = $(this).attr("id");
        $("#hotspot .hot-location").find(".hot-item").css("border", "2px solid #216797");
        $("#hotspot .hot-location").find(".hot-item-pointer").css("background-color", "#216797");
        $(this).css("border", "2px solid #FF6666");
        $(this).find(".hot-item-pointer").css("background-color", "#FF6666");
        if (id.indexOf("_") >= 0) {
            $("#dateModelView")[0].contentWindow.showHotRef(id.split("_")[0]);
        } else {
            $("#dateModelView")[0].contentWindow.showHotRef(id);
        }
    })

    /* 缩小 */
    $("body").on("click", ".zoom-minus", function () {
        var scale = parseInt($(".zoomm-value").text().replace("%", ""));
        if (scale > 10) {
            scale = scale - 10;
            $(".zoomm-value").text(parseInt(scale) + "%");
            zoom(scale / 100);
        }
    })
    /* 放大 */
    $("body").on("click", ".zoom-add", function () {
        var scale = parseFloat($(".zoomm-value").text().replace("%", ""));
        if (scale < 400) {
            scale = scale + 10;
            $(".zoomm-value").text(parseInt(scale) + "%");
            zoom(scale / 100);
        }
    })

    //svg缩小
    $(".svg-minus").on("click", function () {
        //alert("hh")
        var panZoomDown = svgPanZoom('#svg-img');
        var scale = parseInt($(".svg-value").text().replace("%", ""));
        if (scale > 100) {
            scale = scale - 10;
            $(".svg-value").text(parseInt(scale) + "%");
            panZoomDown.zoomOut();
        }

    })
    //svg放大
    $(".svg-add").on("click", function () {
        var panZoomUp = svgPanZoom('#svg-img');
        //panZoomDown.zoomOut();
        var scale = parseFloat($(".svg-value").text().replace("%", ""));
        if (scale < 400) {
            scale = scale + 10;
            $(".svg-value").text(parseInt(scale) + "%");
            panZoomUp.zoomIn();
        }

    })

})

function zoom(scale) {
    var image = new Image();
    image.src = $(".hot-img").attr("src");
    var img = imgOnload(image);
    var realWidth = img.width; //获取图片真实宽度
    var realHeight = img.height; //获取图片真实高度
    $(".hot-img").css("width", realWidth * scale)
}

/* 图片拖拽 */

//传入的必须是jQuery对象，否则不能调用jQuery的自定义函数
function drag() {

    $(".hot-img-div").bind("mousedown", start);

    function start(event) {
        if (event.which == 1) { //判断是否点击鼠标左键
            /*
             * clientX和clientY代表鼠标当前的横纵坐标
             * offset()该方法返回的对象包含两个整型属性：top 和 left，以像素计。此方法只对可见元素有效。
             * bind()绑定事件，同样unbind解绑定，此效果的实现最后必须要解绑定，否则鼠标松开后拖拽效果依然存在
             * getX获取当前鼠标横坐标和对象离屏幕左侧距离之差（也就是left）值，
             * getY和getX同样道理，这两个差值就是鼠标相对于对象的定位，因为拖拽后鼠标和拖拽对象的相对位置是不变的
             */

            gapX = event.clientX - $(".hot-img-div").offset().left;
            gapY = event.clientY - $(".hot-img-div").offset().top + $(document).scrollTop();
            //movemove事件必须绑定到$(document)上，鼠标移动是在整个屏幕上的
            $(document).bind("mousemove", move);
            //此处的$(document)可以改为obj
            $(document).bind("mouseup", stop);

        }
        return false; //阻止默认事件或冒泡
    }

    function move(event) {
        $(".hot-img-div").css({
            "left": (event.clientX - gapX) + "px",
            "top": (event.clientY - gapY) + "px"
        });
        return false; //阻止默认事件或冒泡
    }

    function stop() {
        //解绑定，这一步很必要，前面有解释
        $(document).unbind("mousemove", move);
        $(document).unbind("mouseup", stop);

    }
}

function hotCentre(id) {
    var windowW = $(window).width() / 2 - 41; //获取当前窗口宽度
    var windowH = $(window).height() - 60; //获取当前窗口高度
    var h = $("#" + id).css("top").substring(0, $("#" + id).css("top").indexOf("px"));
    var w = $("#" + id).css("left").substring(0, $("#" + id).css("left").indexOf("px"));
    var imgH = $("#" + id).siblings("img").height();
    var imgW = $("#" + id).siblings("img").width();
    var hotH = $("#" + id).height();
    var hotW = $("#" + id).width();
    $(".hot-img-div").css({
        "top": (windowH - imgH) / 2 + imgH / 2 - h - hotH / 2 + 60,
        "left": (windowW - imgW) / 2 + imgW / 2 - w - hotW / 2
    });
}

var oneHid = false;
var twoHid = false;

function leftS(e) {
    oneHid = e;
    twoHid = e;
}

function hotImgShow(src, hot, _this) {

    if ($("#one").is(":hidden") && $("#hotspot").is(":hidden")) {
        oneHid = true;
    }
    if ($("#two").is(":hidden") && $("#hotspot").is(":hidden")) {
        twoHid = true;
    }
    $("#one").hide();
    $("#two").hide();


    $("#hotspot").find(".hot-img").attr("src", src);

    $("#three").width($(window).width() * 0.49);
    $("#hotspot").css("width", $(window).width() * 0.48);

    /* 打开放大图 */
    $("body").on("click", ".hot_img_zoom", function () {
        parent.imgShow("#outerdiv", "#innerdiv", "#bigimg", src);
    })

    //hot
    $(".hot-location").empty();
    if ($(".hot-location .hot-item").length <= 0) {
        for (var i = 0; i < hot.length; i++) {
            if (hot[i].objectdescr.indexOf("|") >= 0) {
                var hotArr = hot[i].objectdescr.split("|");
                for (var j = 0; j < hotArr.length; j++) {
                    $(".hot-location").append('<span id="' + hot[i].id + "_" + j + 1 + '" class="hot-item" objectdescr="' + hotArr[j] +
                        '" style="' + hotArr[j] + '"><div class="hot-item-pointer"></div></span>');
                }
            } else {
                $(".hot-location").append('<span id="' + hot[i].id + '" class="hot-item" objectdescr="' + hot[i].objectdescr +
                    '" style="' + hot[i].objectdescr + '"><div class="hot-item-pointer"></div></span>');
            }
        }
    }


    var image = new Image();
    image.src = src;
    var img = imgOnload(image);
    var windowW = $(window).width() / 2 - 41; //获取当前窗口宽度
    var windowH = $(window).height() - 60; //获取当前窗口高度
    //var windowW = document.body.clientWidth/2;//获取当前窗口宽度
    //var windowH = document.body.clientHeight;//获取当前窗口高度

    // var realWidth = img.width; //获取图片真实宽度
    // var realHeight = img.height; //获取图片真实高度

    //获取图片真实宽度--因为变形，所以真是宽高获取用户自定义宽高
    var realWidth = _this.width();
    var realHeight = _this.height();
    if($(_this).attr("width")){
        realWidth = $(_this).attr("width");
        realHeight = $(_this).attr("height");
    }else{
        realWidth = _this.width();
        realHeight = _this.height();
    }
    var imgWidth, imgHeight;

    /* if (realHeight > realWidth) {//判断图片高度
        imgHeight = windowH;//如大于窗口高度，图片高度进行缩放
        imgWidth = imgHeight/realHeight*realWidth;// 等比例缩放宽度

        if(imgWidth>windowW){
            imgWidth = windowW;//如大于窗口宽度，图片宽度进行缩放
            imgHeight = imgWidth/realWidth*realHeight;// 等比例缩放高度
        }
    } else if (realWidth > realHeight) {//如图片高度合适，判断图片宽度
        imgWidth = windowW;//如大于窗口宽度，图片宽度进行缩放
        imgHeight = imgWidth/realWidth*realHeight;// 等比例缩放高度
        if(imgHeight>windowH){
            imgHeight = windowH;//如大于窗口高度，图片高度进行缩放
            imgWidth = imgHeight/realHeight*realWidth;// 等比例缩放宽度
        }
    } else {//如果图片真实高度和宽度都符合要求，高宽不变
        imgWidth = windowW;
        imgHeight = windowH;
    } */

    //imgHeight = realHeight;
    //初始化显示放大到150%
    imgWidth = realWidth * 1.5;
    imgHeight = imgWidth / realWidth * realHeight;
    $(".zoomm-value").text("150%");

    $(".hot-img").css("width", imgWidth); //以最终的宽度对图片缩放

    var w = (windowW - imgWidth) / 2; // 计算图片与窗口左边距
    var h = (windowH - imgHeight) / 2; // 计算图片与窗口上边距
    $(".hot-img-div").css({
        "top": h + 60,
        "left": w
    });

    $("#hotspot").css({"display": "inline-table", "position": "absolute", "z-index": "1000"});
    drag();
    //xtt把on改成one点击一次
    $("body").one("click", ".hot_img_close", function () { //再次点击淡出消失弹出层
        //$(this).fadeOut("fast");//cll ie8下跟position fixed 出现图片闪烁

        $("#hotspot").hide();
        $("#dateModelView")[0].contentWindow.closeHot();
        $("#hotspot .hot-location").find(".hot-item").css("border", "2px solid #216797");
        $("#hotspot .hot-location").find(".hot-item-pointer").css("background-color", "#216797");
        initWidth();
        if (oneHid) {
            parent.changeButtonLeft();
            hideLeft();
        } else {
            parent.changeButtonRight();
            showLeft();
        }
        if (twoHid) {
            parent.changeButtonLeft();
            hideLeft();
        } else {
            parent.changeButtonRight();
            showLeft();
        }
        //xtt修改关闭图片慢的bug
        return false;
    });
}


//xtt打开svg弹窗
function openSVG(src, hotId) {
    //console.log("1")
    if ($("#one").is(":hidden") && $("#hotspot").is(":hidden")) {
        oneHid = true;
    }
    if ($("#two").is(":hidden") && $("#hotspot").is(":hidden")) {
        twoHid = true;
    }
    $("#one").hide();
    $("#two").hide();

    var oldSrc = $("#svghotspot").find(".svg-img").attr("data");
    if (src != oldSrc) {
        $("#svghotspot").find(".svg-img").attr("data", src);
        $("#svghotspot").find(".svg-img").attr("hotid", hotId);
    }
    $("#three").width($(window).width() * 0.49);
    $("#svghotspot").css("width", $(window).width() * 0.48);
    $("#svghotspot").css({"display": "inline-table", "position": "absolute", "z-index": "1000"});

}


function openSVGbyhotText(src, svgHotID, hotId) {
    //console.log("2")
    $("#one").hide();
    $("#two").hide();

    var oldSrc = $("#svghotspot").find(".svg-img").attr("data");
    if (src != oldSrc) {
        $("#svghotspot").find(".svg-img").attr("data", src);
        $("#svghotspot").find(".svg-img").attr("hotid", hotId);
    }

    $("#three").width($(window).width() * 0.49);
    $("#svghotspot").css("width", $(window).width() * 0.48);
    $("#svghotspot").css({"display": "inline-table", "position": "absolute", "z-index": "1000"});

    if (svgHotID != null && svgHotID != '' && svgHotID != 'null') {
        showSVGHot(svgHotID);
    }

}


//初始化svg插件
function zoomSvg(obj) {
    // 此处获取的元素Id是SVG文件中的<g>标签的id值
    //$(obj.getSVGDocument().getElementsByTagName("g")[0]).show();
    // $(obj.getSVGDocument().getElementById('svg_2')).show();
    svgPanZoom(obj, {
        zoomEnabled: false, //开启缩放功能
        controlIconsEnabled: false, //开启右下角缩放控件功能
        zoomScaleSensitivity: 0.1, //每次缩放大小
        dblClickZoomEnabled: false
    });

    //svg图片元素添加点击事件
    var svg = obj.getSVGDocument();
    //console.log(svg)
    svgClick(svg);
}

//关闭svg图片小弹窗
function closeSvg() {
    $("#svghotspot").hide();
    $("#dateModelView")[0].contentWindow.closeHot();
    initWidth();
    if (oneHid) {
        parent.changeButtonLeft();
        hideLeft();
    } else {
        parent.changeButtonRight();
        showLeft();
    }
    if (twoHid) {
        parent.changeButtonLeft();
        hideLeft();
    } else {
        parent.changeButtonRight();
        showLeft();
    }
}

//svg全屏显示
function svgShow() {
    //alert("hh");
    var src = $("#svghotspot").find(".svg-img").attr("data");
    //return false;
    //console.log(src);
    parent.svgShow("#svgouterdiv", "#bigsvg", src);
}

//svg部位高亮显示
function linkGroup(groupFlag, pathList) {
    for (var i = 0; i < pathList.length; i++) {
        if (pathList[i].getAttribute("group") == groupFlag) {
            pathList[i].setAttribute("fill", "#ff0000");
        } else if (pathList[i].getAttribute("group") != null) {
            pathList[i].setAttribute("fill", "#ffffff");
        }
    }
}

//传入SVG热点id，高亮显示svg热点元素
function showSVGHot(svgHotID) {
    //zoomSvg(document.getElementById("svg-img"));
    var svg = document.getElementById("svg-img").getSVGDocument();
    //console.log(svg)
    if (svg != null) {
        //var pathList = svg.getElementsByTagName("path");
        //清空之前热点高亮显示
        var textList = svg.getElementById("Hostop").getElementsByTagName("text");
        for (var i = 0; i < textList.length; i++) {
            textList[i].setAttribute("fill", "#000000")
        }
        //console.log(svg.getElementsByTagName("svg"));
        // for (var i = 0; i < pathList.length; i++) {
        // 	// console.log(pathList[i].getAttribute('stroke'));//none,#000000
        // 	var strokeStr = pathList[i].getAttribute('stroke');
        // 	var fillStr = pathList[i].getAttribute('fill');
        // 	if (strokeStr == "none" && fillStr == "#00aa00") {
        // 		pathList[i].setAttribute("fill", "#ffffff");
        // 	}
        // 	if (strokeStr == "none" && fillStr == "#ff0000") {
        // 		pathList[i].setAttribute("fill", "#ffffff");
        // 	}
        // 	if (strokeStr == "none" && fillStr == "#0055ff") {
        // 		pathList[i].setAttribute("fill", "#ffffff");
        // 	}
        // }
        svgClick(svg);
        zoomSvg(document.getElementById("svg-img"));
        console.log(svgHotID)
        if (svgHotID.indexOf(';') > -1) {
            var ids = svgHotID.split(";");
            if (ids.length > 0) {
                for (var i = 0; i < ids.length; i++) {
                    var id = ids[i].substring(ids[i].length - 4, ids[i].length);
                    //console.log(id);
                    svg.getElementById(id).setAttribute("fill", "#ff0000");
                }
            }

        } else {
            var id = svgHotID.substring(svgHotID.length - 4, svgHotID.length);
            //console.log(id);
            svg.getElementById(id).setAttribute("fill", "#ff0000");
        }

        //console.log(svg.getElementById(svgHotID));
        //pathList[svgHotID]

    } else {
        document.getElementById("svg-img").onload = function () {
            showSVGHot(svgHotID);
        }
    }


}

//重新刷新svg
// function resetImg() {
// 	var panZoomRes = svgPanZoom('#svg-img');
// 	panZoomRes.reset();
// 	var svg = document.getElementById("svg-img").getSVGDocument();
// 	var pathList = svg.getElementsByTagName("path");
// 	console.log(svg.getElementsByTagName("svg"));
// 	for (var i = 0; i < pathList.length; i++) {
// 		// console.log(pathList[i].getAttribute('stroke'));//none,#000000
// 		var strokeStr = pathList[i].getAttribute('stroke');
// 		var fillStr = pathList[i].getAttribute('fill');
// 		if (strokeStr == "none" && fillStr == "#00aa00") {
// 			pathList[i].setAttribute("fill", "#ffffff");
// 		}
// 		if (strokeStr == "none" && fillStr == "#0055ff") {
// 			pathList[i].setAttribute("fill", "#ffffff");
// 		}
// 		if (strokeStr == "none" && fillStr == "#ff0000") {
// 			pathList[i].setAttribute("fill", "#ffffff");
// 		}
// 	}
// 	// document.location.reload();
// }

//为svg元素添加点击事件
function svgClick(svg) {
    // var pathList = svg.getElementsByTagName("path");
    // for (var i = 0; i < pathList.length; i++) {
    // 	var strokeStr = pathList[i].getAttribute('stroke');
    // 	var fillStr = pathList[i].getAttribute('fill');
    // 	if (strokeStr == "none" && fillStr == "#ffffff") {
    // 		// console.log(i);
    // 		//pathList[i].setAttribute("id", i);
    // 		// if (i % 2 == 0) {
    // 		// 	pathList[i].setAttribute("group", "doubleId");
    // 		// } else {
    // 		// 	pathList[i].setAttribute("group", "singleId");
    // 		// }
    // 		pathList[i].onmousemove = function() {
    // 			if(this.getAttribute("fill")!="#ff0000"){
    // 				this.setAttribute("fill", "#0055ff");
    // 			}
    // 		}
    // 		pathList[i].onmouseout = function() {
    // 			if(this.getAttribute("fill")!="#ff0000"){
    // 				this.setAttribute("fill", "#ffffff");
    // 			}
    // 		}
    // 		pathList[i].setAttribute("c", pathList[i].getAttribute("id"));
    // 		// pathList[i].setAttribute("g", pathList[i].getAttribute("group"));
    // 		pathList[i].addEventListener("click", function a() {
    // 			console.log("id的值是：" + this.getAttribute("c"));
    // 			// console.log("group的值是：" + this.getAttribute("g"));
    // 			//所有group 的值相同的标签改变颜色
    // 			//this.getAttribute("fill")!="#ff0000";
    // 			for (var i = 0; i < pathList.length; i++) {
    // 				// console.log(pathList[i].getAttribute('stroke'));//none,#000000
    // 				var strokeStr = pathList[i].getAttribute('stroke');
    // 				var fillStr = pathList[i].getAttribute('fill');
    // 				if (strokeStr == "none" && fillStr == "#00aa00") {
    // 					pathList[i].setAttribute("fill", "#ffffff");
    // 				}
    // 				if (strokeStr == "none" && fillStr == "#ff0000") {
    // 					pathList[i].setAttribute("fill", "#ffffff");
    // 				}
    // 				if (strokeStr == "none" && fillStr == "#0055ff") {
    // 					pathList[i].setAttribute("fill", "#ffffff");
    // 				}
    // 			}
    // 			pathList[this.getAttribute("c")].setAttribute("fill","#ff0000");
    // 			//linkGroup(this.getAttribute("g"), pathList);
    // 			// alert(pathList[i].getAttribute("id")) ;
    // 			$("#dateModelView")[0].contentWindow.showSVGHotRef(this.getAttribute("c"));

    // 		})
    // 	}
    // }

    var textList = svg.getElementById("Hostop").getElementsByTagName("text");
    var lista = document.getElementById("lista");
    var alist = document.getElementsByTagName("a");
    for (var i = 0; i < textList.length; i++) {
        // textList[i].setAttribute("id", PrefixInteger(i, 4));
        textList[i].setAttribute("id", PrefixInteger(i + 1, 4));
        //添加鼠标悬浮
        textList[i].onmousemove = function () { //#00aa00
            if (this.getAttribute("fill") != "#ff0000") {
                this.setAttribute("fill", "#0055ff");
            }
        }
        textList[i].onmouseout = function () {
            if (this.getAttribute("fill") != "#ff0000") {
                this.setAttribute("fill", "#000000");
            }
        }
        textList[i].setAttribute('i', textList[i].getAttribute("id"));
        textList[i].onclick = function () {
            //清空之前热点高亮显示
            for (var i = 0; i < textList.length; i++) {
                textList[i].setAttribute("fill", "#000000")
            }
            // console.log(this.getAttribute("i"))
            this.setAttribute("fill", "#ff0000");
            console.log("写入id文本" + this.getAttribute("i"))
            //myArray.push(this.getAttribute("i"));
            //document.getElementById(this.getAttribute("i")).style.cssText = "color:red;display:block";
            console.log($("#svg-img").attr("hotid") + "-hot-");
            $("#dateModelView")[0].contentWindow.showSVGHotRef($("#svg-img").attr("hotid") + "-hot-" + this.getAttribute("i"));
        }
    }
}

function PrefixInteger(num, m) {
    return (Array(m).join(0) + num).slice(-m);
}

